package br.com.webagendatelefonica;

import java.io.IOException;
import static java.lang.System.out;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SLCadastrar extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, ClassNotFoundException {
        response.setContentType("text/html;charset=UTF-8");
        String erro = "";
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            String url = "jdbc:oracle:thin:@localhost:1521:XE";
            Connection cn = DriverManager.getConnection(url, "agenda", "123");
            PreparedStatement pstm = cn.prepareStatement("INSERT INTO AGENDA.CONTATO (CONTATO_ID, NOME, ENDERECO, TELEFONE1, TELEFONE2, CELULAR, RADIO) \n"
                    + "VALUES (?,?,?,?,?,?,?)");
            pstm.setInt(1, Integer.parseInt(request.getParameter("ID")));
            pstm.setString(2, request.getParameter("NOME"));
            pstm.setString(3, request.getParameter("ENDERECO"));
            pstm.setInt(4, Integer.parseInt(request.getParameter("TELEFONE1")));
            pstm.setInt(5, Integer.parseInt(request.getParameter("TELEFONE2")));
            pstm.setInt(6, Integer.parseInt(request.getParameter("CELULAR")));
            pstm.setString(7, request.getParameter("RADIO"));
            pstm.executeUpdate();
            cn.close();

        } catch (ClassNotFoundException | SQLException e) {
            out.println(e.getMessage());
        }
        if (erro.equals("")) {
            out.println("Contato cadastrado com sucesso!");
        } else {
            out.println(erro);
        }
    }

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);

        } catch (ClassNotFoundException ex) {
            Logger.getLogger(SLCadastrar.class
                    .getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);

        } catch (ClassNotFoundException ex) {
            Logger.getLogger(SLCadastrar.class
                    .getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
